package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.exception.DeletionNotAllowedException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.entity.Clazz;
import com.itheima.pojo.dto.ClazzQuery;
import com.itheima.pojo.entity.Emp;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.entity.Student;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper,Clazz> implements ClazzService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Clazz> page(ClazzQuery clazzQuery) {
        Page<Clazz> page = Page.of(clazzQuery.getPage(), clazzQuery.getPageSize());
        LambdaQueryWrapper<Clazz> wrapper = new LambdaQueryWrapper<Clazz>().like(clazzQuery.getName() != null && !clazzQuery.getName().isEmpty(), Clazz::getName, clazzQuery.getName())
                .between(clazzQuery.getBegin() != null && clazzQuery.getEnd() != null, Clazz::getEndDate, clazzQuery.getBegin(), clazzQuery.getEnd())
                .orderByDesc(Clazz::getUpdateTime);
        this.page(page,wrapper);
        List<Emp> empList = empMapper.selectList(new LambdaQueryWrapper<Emp>().select(Emp::getId, Emp::getName));
        if(!CollectionUtil.isEmpty(empList)){
            for (Clazz clazz : page.getRecords()) {
                for (Emp emp : empList) {
                    if(emp.getId().equals(clazz.getMasterId())){
                        clazz.setMasterName(emp.getName());
                    }
                }

            }
            page.getRecords().forEach(
                    clazz -> {
                        if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                            clazz.setStatus("未开班");
                        } else if (clazz.getBeginDate().isBefore(LocalDate.now())
                                && clazz.getEndDate().isAfter(LocalDate.now())) {
                            clazz.setStatus("进行中");
                        } else {
                            clazz.setStatus("已结束");
                        }
                    }
            );
        }

        return new PageResult<>(page.getTotal(), page.getRecords());
        /*// 开启分页
        PageHelper.startPage(clazzQuery.getPage(), clazzQuery.getPageSize());
        //执行查询
        List<Clazz> clazzList = clazzMapper.page(clazzQuery);
        Page< Clazz> pageList = (Page< Clazz>) clazzList;
        pageList.forEach(clazz->{
            if(clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开班");
            }else if(clazz.getBeginDate().isBefore(LocalDate.now())
                    &&clazz.getEndDate().isAfter(LocalDate.now())){
                clazz.setStatus("进行中");
            }else {
                clazz.setStatus("已结束");
            }
        });
        return new PageResult<Clazz>(pageList.getTotal(), pageList.getResult());*/

    }

    @Override
    public void delete(Integer id) {
        List<Student> studentList=studentMapper.selectList(new LambdaQueryWrapper<Student>().in(Student::getClazzId, id));
        if(!CollectionUtil.isEmpty(studentList)){
            throw new DeletionNotAllowedException("班级下有学生，不能删除!");
        }
        removeById(id);
    }

    @Override
    public Clazz findById(Integer id) {
        Clazz clazz = getById(id);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }


    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        List<Clazz> clazzList = list();
        return clazzList;
    }
}
